import zipfile
import os


class zip_operate(object):
    '''
    解析zip压缩包
    '''
    def __init__(self,zip_file_path=None):
        self.zip_path=zip_file_path

    def file_list_info(self):
        '''
        获取压缩包中文件内容，并且以dict方式返回
        :return:
        '''
        items={}
        self.check_zip()
        zip_obj=zipfile.ZipFile(self.zip_path,'r')
        for sub in zip_obj.namelist():
            items[sub]=zip_obj.read(sub).decode('utf-8')
        filename_list=os.path.split(zip_obj.filename)
        filename=filename_list[-1]
        project_name=filename.split('.')[0]
        items['project_name']=project_name
        return items


    def check_zip(self):
        '''
        检测文件是否是zip文件
        :return:
        '''
        if zipfile.is_zipfile(self.zip_path)==False:
            raise Exception('%s 不是zip文件' %self.zip_path)






if __name__ == '__main__':
    path=os.path.join(os.path.dirname(os.path.dirname(__file__)),'example','疯狂迷宫.mt')
    zip_obj=zip_operate(zip_file_path=path)
    zip_obj.file_list_info()